package Views;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import BusinessObjects.ServerMessage;
import Models.ServerConnexionModel;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ServerCommunicationView extends JFrame implements Observer {
	private JLabel alert;
	private ServerConnexionModel model;
	private JTextField textFieldRequest;
	private JTextField textFieldService;
	private JTextArea textAreaAnswer;
	
	public ServerCommunicationView(ServerConnexionModel model, ActionListener controller, String name) {
		this.model = model;
		model.addObserver(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 350);
		JPanel contentPane = new JPanel();
		contentPane.setBackground(new Color(204, 204, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		contentPane.setLayout(null);
		setTitle(name);
			
		setContentPane(contentPane);
		
		JLabel lblLogin = new JLabel("Communication au serveur");
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblLogin.setBounds(10, 11, 306, 22);
		contentPane.add(lblLogin);
		
		alert = new JLabel("");
		alert.setForeground(Color.RED);
		alert.setBounds(10, 245, 351, 22);
		contentPane.add(alert);
		
		textFieldRequest = new JTextField();
		textFieldRequest.setBounds(10, 113, 253, 20);
		contentPane.add(textFieldRequest);
		textFieldRequest.setColumns(10);
		
		JLabel lblRequteAuServeur = new JLabel("Requ\u00EAte au serveur :");
		lblRequteAuServeur.setBounds(10, 88, 240, 14);
		contentPane.add(lblRequteAuServeur);
		
		JLabel lblRponse = new JLabel("R\u00E9ponse :");
		lblRponse.setBounds(10, 144, 240, 14);
		contentPane.add(lblRponse);
		
		JButton buttonEnvoye = new JButton("Envoyer!");
		buttonEnvoye.setBounds(273, 112, 89, 23);
		contentPane.add(buttonEnvoye);
		buttonEnvoye.addActionListener(controller);
		buttonEnvoye.setActionCommand("sendRequest");
		
		textFieldService = new JTextField();
		textFieldService.setBounds(79, 57, 184, 20);
		contentPane.add(textFieldService);
		textFieldService.setColumns(10);
		
		JLabel lblService = new JLabel("Service :");
		lblService.setBounds(10, 60, 59, 14);
		contentPane.add(lblService);
		
		JButton buttonBack = new JButton("<< Retour");
		buttonBack.setBounds(10, 278, 89, 23);
		contentPane.add(buttonBack);
		buttonBack.addActionListener(controller);
		buttonBack.setActionCommand("back");
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 169, 351, 65);
		contentPane.add(scrollPane);
		
		textAreaAnswer = new JTextArea();
		textAreaAnswer.setEditable(false);
		textAreaAnswer.setEnabled(false);
		textAreaAnswer.setLineWrap(true);
		textAreaAnswer.setWrapStyleWord(true);
		textAreaAnswer.setDisabledTextColor(Color.BLACK);
		scrollPane.setViewportView(textAreaAnswer);
	
		setVisible(true);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		alert.setText(model.getAlert());
		textAreaAnswer.setText(model.getAnswer());
	}
	
	public ServerMessage getServerMessageInfos() {
		return new ServerMessage(0, textFieldRequest.getText(), textFieldService.getText());
	}
	
	public void changeViewInfoBox(String message) {
		JOptionPane.showMessageDialog(this, message);
	}
}
